//
//  IpInputViewController.m
//  Dolphin
//
//  Created by alex tao on 3/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "IpInputViewController.h"
#import "DolphinAppDelegate.h"

@interface IpInputViewController ()

@end

@implementation IpInputViewController

@synthesize block = _block;

+ (BOOL)isIPAddress:(NSString*)address 
{
	BOOL isIPAddress = NO;
    if (address && address.length > 0) {
        NSArray *components = [address componentsSeparatedByString:@"."];
        NSCharacterSet *invalidCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet]; 
        
        if ([components count] == 4) {
            NSString *part1 = [components objectAtIndex:0];
            NSString *part2 = [components objectAtIndex:1];
            NSString *part3 = [components objectAtIndex:2];
            NSString *part4 = [components objectAtIndex:3];
            
            if ([part1 rangeOfCharacterFromSet:invalidCharacters].location == NSNotFound &&
                [part2 rangeOfCharacterFromSet:invalidCharacters].location == NSNotFound &&
                [part3 rangeOfCharacterFromSet:invalidCharacters].location == NSNotFound &&
                [part4 rangeOfCharacterFromSet:invalidCharacters].location == NSNotFound ) {
                
                if ([part1 intValue] < 255 &&
                    [part2 intValue] < 255 &&
                    [part3 intValue] < 255 &&
                    [part4 intValue] < 255) {
                    isIPAddress = YES;
                }
            }
        }
    }
	
	return isIPAddress;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    DolphinAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    if (appDelegate.ipAddress && appDelegate.port > 0) {
        _ipText.text = appDelegate.ipAddress;
        _portText.text = [NSString stringWithFormat:@"%d", appDelegate.port];
    }
}

- (void)viewDidUnload
{
    [_ipText release];
    _ipText = nil;
    [_portText release];
    _portText = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (void)dealloc {
    self.block = nil;
    [_ipText release];
    [_portText release];
    [super dealloc];
}

- (IBAction)okClicked 
{
    NSString * strIp = _ipText.text;
    NSString * strPort = _portText.text;
    if (strIp && strPort) {
        NSScanner * intScan = [NSScanner scannerWithString:strPort];
        int port = 0;
        if ([IpInputViewController isIPAddress:strIp] && [intScan scanInt:&port] && port > 0) {
            DolphinAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
            appDelegate.ipAddress = strIp;
            appDelegate.port = port;
            if (_block) {
                _block();
            }
            return;
        }
    }
    
    UIAlertView * alView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"IP地址或端口填写错误" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
    [alView show];
    [alView release];
}

@end
